<?php
/**
 * Request
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use framework\library\interfaces\Request_Interface;
/**
 * Request Class
 *
 * Plurious Core
 *
 * Cleans and stores all inbound data from the superglobal arrays
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 */
class Request implements Request_Interface {

    /** Holds the $_GET contents - use getGet() */
    private $get = array();

    /** Holds the $_POST contents - use getPost() */
    private $post = array();

    /** Holds the $_COOKIE contents - use getCookie() */
    private $cookie = array();

    /** Holds the $_FILES contents - use getFiles() */
    private $files = array();

    /** Holds the $_SERVER contents - use getReal() */
    private $server = array();

    /**
     * Clean and store all inbound data
     */
    public function __construct() {
        $this->get = $this->clean($_GET);
        $this->post = $this->clean($_POST);
        $this->request = $this->clean($_REQUEST);
        $this->cookie = $this->clean($_COOKIE);
        $this->files = $this->clean($_FILES);
        $this->server = $this->clean($_SERVER);
        if (strpos($this->getReal('server', 'SERVER_SOFTWARE'), 'IIS')) {
            $this->setupWindowsIISCompatibility();
        }
    }

    /**
     * Recursive function that adds htmlspecialchars treatment to each REQUEST array
     * 
     * @param mixed $data
     * @return mixed cleaned $data
     */
    public function clean($data) {
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                unset($data[$key]);
                $data[$this->clean($key)] = $this->clean($value);
            }
        } else {
            $data = htmlspecialchars($data, ENT_COMPAT);
        }
        return $data;
    }

    /**
     * Get a value originally stored in the $_GET Superglobal
     * 
     * @param string $value to retrieve
     */
    public function getGet($value) {
        return $this->getReal('get', $value);
    }

    /**
     * Get a value originally stored in the $_REQUEST Superglobal
     * 
     * @param string $value to retrieve
     */
    public function getRequest($key, $value) {
        return $this->getReal($key, $value);
    }

    /**
     * Get a value originally stored in the $_POST Superglobal
     * 
     * @param string $value to retrieve
     */
    public function getPost($value) {
        return $this->getReal('post', $value);
    }

    /**
     * Get a value originally stored in the $_COOKIE Superglobal
     * 
     * @param string $value to retrieve
     */
    public function getCookie($value) {
        return $this->getReal('cookie', $value);
    }

    /**
     * Get a value originally stored in the $_FILES Superglobal
     * 
     * @param string $value to retrieve
     */
    public function getFiles($value) {
        return $this->getReal('files', $value);
    }

    /**
     * Get a value originally stored in the $_SERVER Superglobal
     * 
     * @param string $value to retrieve
     */
    public function getServer($value) {
        return $this->getReal('server', $value);
    }

    /**
     * Sets SERVER properties in a consistent manner.
     */
    private function setupWindowsIISCompatibility() {
        $docRoot = $this->getReal('server', 'DOCUMENT_ROOT');
        $phpSelf = $this->getReal('server', 'PHP_SELF');
        if (!$docRoot) {
            $scriptName = $this->getReal('server', 'SCRIPT_FILENAME');
            $pathTranslated = $this->getReal('server', 'PATH_TRANSLATED');
            if ($scriptName) {
                $this->server['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($scriptName, 0, 0 - strlen($phpSelf)));
            } elseif ($pathTranslated) {
                $this->server['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $pathTranslated), 0, 0 - strlen($phpSelf)));
            }
        }
        $reqUri = $this->getReal('server', 'REQUEST_URI');
        $qryString = $this->getReal('server', 'QUERY_STRING');
        if (!$reqUri) {
            $reqUri = substr($phpSelf, 2);
            if ($qryString) {
                $reqUri .= '?' . $qryString;
            }
            $this->server['REQUEST_URI'] = $reqUri;
        }
    }

    /**
     * The single retrieval function that draws from 
     * each of the arrays as necessary.
     * 
     * @param mixed $key The Superglobal array name (get, post, server, files, etc.)
     * @param mixed $value The value or null if not set
     */
    private function getReal($key, $value) {
        if (isset($this->{$key}[$value])) {
            return $this->{$key}[$value];
        } else {
            return null;
        }
    }

}

?>